﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _0712502_0712504_DesignPatternGame
{
    /// <summary>
    /// Lớp ảo 
    /// </summary>
    public abstract class AutomatedTank : TankPrototype
    {        
        /// <summary>
        /// Các thuộc tính riêng của lớp Automated tank
        /// </summary>

        public static bool _isClocked = false;
        protected int _timeClocked = 0;
       
        protected int _timeToChangeDirection;  // Thoi gian chuyen huong di      
        protected int _timeNow;       
        protected int _timeToShot;    // thoi gian de ban        
        protected int _timeToReload;             // thoi gian de reload       
        protected int _timeShot;    // thoi gian ca.p nha.t de thuc hien ban
       
        public HealthyBarDrawer healthyBar;
        protected int _expMark;  // điểm kinh nghiệm khi jết đc tank
        public int ExpMark
        {
            get { return _expMark; }
            set { _expMark = value; }
        }

        int _maxSpeed;
        public int MaxSpeed
        {
            get { return _maxSpeed; }
            set { _maxSpeed = value; }
        }

        /// <summary>
        /// Các phương thức riêng của lớp Automated tank
        /// </summary>
        public AutomatedTank()
        {
            _timeNow = 0;
            _timeToReload = 0;
            _timeClocked = 0;
            _isClocked = false;           
        }

   
        /// Các phương thức tự động của Automated tank
        public void AutoShot()
        {            
            _timeToShot++;
            if (_timeToShot >= _timeShot)
            {
                Random rand = new Random(DateTime.Now.Millisecond);
                int num = rand.Next(0, 50);
                if (num % _timeToShot == 0)
                {
                    TankShot();
                }
                _timeToShot = 0;
            }
        }

        public void AutoMove()      // Ham tu dong di chuyen
        {                       
            if (_isClocked == false)
            {
                _timeNow += 1;
                
                if ((this.CheckCollision()) == true ||
                    _timeNow > _timeToChangeDirection)
                
                {
                    ChangeMovingDirection();
                    _timeNow = 0;
                   
                }

                if (this.MoveState == MovingState.Run)
                {
                    this.Move();
                }
                this.MoveState = MovingState.Run;
            }
                
            else
            {
                _timeClocked++;
                if (_timeClocked > 1000)
                {
                    _timeClocked = 0;
                    _isClocked = false;
                }
            }
        }

        public void ChangeMovingDirection()
        {
            Random rand = new Random(DateTime.Now.Millisecond);
            int stateRand = rand.Next(0, 100);
            stateRand = stateRand % 4;
            for (int i = 0; i < 4; i++)
            {
                stateRand += i;
                this.MovingDirection = (MoveDirectionState)(stateRand % 4);
                
                if (CheckCollision() == false)
                {
                    return;
                }
            }
            this.MoveState = MovingState.Stop;
            
        }
        
        public override void Move()
        {
            if (this.MovingDirection == MoveDirectionState.left)
            {
                BodyRotationAngle = MathHelper.ToRadians(270.0f);
                TurretRotationAngle = MathHelper.ToRadians(270.0f);
            }
            if (this.MovingDirection == MoveDirectionState.right)
            {
                BodyRotationAngle = MathHelper.ToRadians(90.0f);
                TurretRotationAngle = MathHelper.ToRadians(90.0f);
            }

            if (this.MovingDirection == MoveDirectionState.down)
            {
                BodyRotationAngle = MathHelper.ToRadians(0.0f);
                TurretRotationAngle = MathHelper.ToRadians(0.0f);
            }
            if (this.MovingDirection == MoveDirectionState.down)
            {
                BodyRotationAngle = MathHelper.ToRadians(180.0f);
                TurretRotationAngle = MathHelper.ToRadians(180.0f);
            }

            base.Move();
        }


        public override void TankUpdate(GameTime gameTime)
        {
            this.AutoMove();
            
            base.TankUpdate(gameTime);
            this.TurretTank.RotationAngle = this.RotationAngle;
            this.BodyTank.RotationAngle = this.RotationAngle;
            this.AutoShot();
            this._bulletManager.Update(gameTime);
         
            this.healthyBar.Position = new Vector2(this.RectCollisionOutside.X, this.RectCollisionOutside.Y);
        }

        public override void TankShot()
        {
            BulletLevel1 newBullet = new BulletLevel1();
            newBullet.IdTank = this.idTank;
            Vector2 direction = new Vector2(-(float)Math.Sin(-this.TurretTank.RotationAngle), -(float)Math.Cos(-this.TurretTank.RotationAngle));

            newBullet.MovingDirection = this.MovingDirection;
            newBullet.Position = this.TurretPosition + new Vector2(20, 20) * direction;

            newBullet.IdTank = this.idTank;

            newBullet.TotalDamage = newBullet.Damages + this.Dammage;

            this._bulletManager._listBullets.Add(newBullet);

            GameResourceContainer.SoundManager.DestBrick.Play();
        }
    }
}
